package com.eascs.admin.o2o.loan.notify.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.admin.o2o.specialInterview.entity.O2OSpecialInterview;
import com.eascs.admin.o2o.specialInterview.service.O2OSpecialInterviewService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 特殊面签申报流
 */
@Service
public class SPECMQSBMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(SPECMQSBMsgNotifyServiceImpl.class);

	
	@Autowired
	O2OSpecialInterviewService o2OSpecialInterviewService;
	
	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply){
		ModelResult<O2oLoanapplyEntity> modelResult =new ModelResult<>();
		log.info("启动特殊面签申报流程通知:[{}]", StatusContexts.WFTYPE_SPECBRT);
		/*if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {
			log.info("归档流程通过记录通知：{}", loanapply.getBillCode());
			loanapply.setIsPlaceOnFile(StatusContexts.WFTYPE_STATUS_CLO);
			loanapply.setPlaceOnFileTime(new Date());
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {
			log.info("归档流程驳回记录通知：{}", loanapply.getBillCode());
			loanapply.setIsPlaceOnFile(StatusContexts.O2O_NEW);
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {
			log.info("归档流程作废记录通知：{}", loanapply.getBillCode());
		}*/
		updateSpecialInterview(notifyParam);
		modelResult.withModel(loanapply);
		return modelResult;
	}


	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add(ProcessTemplate.PROECESS_TYPE_SPEC_BANK_REPORT);
		return list;
	}
	
	
	@Override
	public Boolean checkReturn() {
		return Boolean.TRUE;
	}
	
	/**
	 * @Description:
	 * 				<p>
	 *               特殊面签申报通知处理
	 *               </p>
	 * @author yy
	 * @date 2017年3月09日 上午10:01:56
	 * @version V1.0
	 */
	public ModelResult<Boolean> updateSpecialInterview(WFNotifyParam notifyParam) {
		O2OSpecialInterview o2OSpecialInterview = o2OSpecialInterviewService.queryEntity(notifyParam.getReferId());
		// 特殊面签申报通过
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {
			log.info("ReferId:[{}],特殊面签申报结束成功", notifyParam.getReferId());
			o2OSpecialInterview.setBillStatus(StatusContexts.O2O_BANK_CLO); // 结束
			int i = o2OSpecialInterviewService.update(o2OSpecialInterview);
			if (i > 0) {
				return new ModelResult<Boolean>().withModel(Boolean.TRUE);
			} else {
				return new ModelResult<>("workflow.notify.insert.error", "特殊面签申报通过通知失败");
			}
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {
			log.info("ReferId:[{}],特殊面签申报结束驳回", notifyParam.getReferId());
			// 特殊面签申报驳回
			o2OSpecialInterview.setBillStatus(StatusContexts.O2O_BANK_NEW);
			int i = o2OSpecialInterviewService.update(o2OSpecialInterview);
			log.info("更新特殊面签申报记录成功或失败：【{}】", i);
			if (i > 0) {
				return new ModelResult<Boolean>().withModel(Boolean.TRUE);
			} else {
				return new ModelResult<>("workflow.notify.insert.error", "回款流程驳回通知失败");
			}
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {
			log.info("ReferId:[{}],回款流程作废", notifyParam.getReferId());
		}
		return new ModelResult<Boolean>().withModel(Boolean.TRUE);
	}
	
}
